﻿using System;
using System.Collections.Generic;
using System.Text;
using DataGeneration.Domain.Helpers;

namespace DataGeneration.Domain.CustomProcessors
{
    /// <summary>
    /// Defines the type RandomMizer
    /// </summary>
    public class RandomMizer
    {
        private static readonly Random Randomizer = new Random(1000);
        private static readonly IList<Month> MonthList = EnumHelper.GetEnumList<Month>();

        /// <summary>
        /// Gets the next value.
        /// </summary>
        /// <param name="minValue">The min value.</param>
        /// <param name="maxValue">The max value.</param>
        /// <returns></returns>
        public static int GetNextValue(int minValue, int maxValue)
        {
            return Randomizer.Next(minValue, maxValue + 1);
        }

        /// <summary>
        /// Gets the random date string.
        /// </summary>
        /// <value>The random date string.</value>
        public static string RandomDateString
        {
            get
            {
                var month = MonthList[Randomizer.Next(0, MonthList.Count)];
                var year = Randomizer.Next(1900, DateTime.Now.Year);

                return GenerateDate(month, year, false);
            }
        }

        /// <summary>
        /// Gets the random date of birth.
        /// </summary>
        /// <value>The random date of birth.</value>
        public static string RandomDateOfBirth
        {
            get
            {
                var month = MonthList[Randomizer.Next(0, MonthList.Count)];
                var year = Randomizer.Next(DateTime.Now.Year - 110, DateTime.Now.Year);

                return GenerateDate(month, year, true);
            }
        }
        /// <summary>
        /// Randoms the string.
        /// </summary>
        /// <param name="maxCharacters">The max characters.</param>
        /// <returns></returns>
        public static string RandomString(int maxCharacters)
        {
            var stringBuilder = new StringBuilder();
            for (var counter = 0; counter < maxCharacters; counter++)
            {
                stringBuilder.Append(StringProcessor.Alphabets[GetNextValue(1, 26).ToString()]);
            }

            return stringBuilder.ToString();
        }

        /// <summary>
        /// Randoms the upper case string.
        /// </summary>
        /// <param name="numberOfCharacters">The number of characters.</param>
        /// <returns></returns>
        public static string RandomUpperCaseString(int numberOfCharacters)
        {
            var stringBuilder = new StringBuilder();
            for (var counter = 0; counter < numberOfCharacters; counter++)
            {
                stringBuilder.Append(StringProcessor.Alphabets[GetNextValue(1, 26).ToString()]);
            }

            return stringBuilder.ToString().ToUpper();
        }

        private static string GenerateDate(Month month, int year, bool dateOfBirth)
        {
            int day;
            switch (month)
            {
                case Month.February:
                    day = Randomizer.Next(1, DateTime.IsLeapYear(year) ? 29 : 28);
                    break;
                case Month.April:
                case Month.June:
                case Month.September:
                case Month.November:
                    day = Randomizer.Next(1, 30);
                    break;
                default:
                    day = Randomizer.Next(1, 31);
                    break;
            }

            if (dateOfBirth && DateTime.Now.Year <= year)
            {
                //lets reduce the year to a value less than this year!
                year -= 1;
            }

            return string.Format("{0} {1} {2}", day, month, year);
        }
    }
}


